<?php
class RegisteSession {
	public $Channel; //1=>表示夏令营，2=>表示冬令营,3=>全日制，4=>莲花
	public $GradeId; //为所报年级的Id
	public $GradeType; //1=>表示报第一期，2=>表示报第二期，3=> 表示两期连报
	public $EditFlag; //可以修改，否则不能修改
	public $StudentId; //学生Id
}
class RegisteAction extends AppAction {
	private $registerSession;
	private $fullTimeLib;
	private $lotusLib;
	private $campLib;
	function __construct() {
		parent::__construct ();
		$this->registerSession = UserSession::getRegisteInfo ();
		require_once APP_LIB_PATH . 'campLib.php';
		require_once APP_LIB_PATH . 'fullTimeLib.php';
		require_once APP_LIB_PATH . 'lotusLib.php';
		$this->campLib = new CampLib ();
		$this->fullTimeLib = new FullTimeLib ();
		$this->lotusLib = new LotusLib ();
	
	}
	/**
	 * 报名：http://apple.zhaosheng.com/index.php/registe/camp?Channel=1&GradeId=1&GradeType=1
	 * channel =1表示夏令营，channel=2表示冬令营,3=>全日制，4=>莲花
	 * gradeid为所报年级的Id
	 * registertype=1 表示报第一期，registertype=2 表示报第二期，registertype=3 表示两期连报
	 * editflag=1可以修改，否则不能修改
	 * session =array('channel'=>1,'gradeid'=>'1','gradetype'=>'1','editflag'='1','studentid'='1');
	 */
	
	/**
	 * 判断是否有$_POST;
	 * 判断用户填写的正确性
	 * 判断图片是否合法
	 * 判断数据库中改用户名和手机号码是否已经存在数据库中
	 * 如果一切都正确，把数据插入到数据库中，并且把插入的Id返回
	 * 把session 重新写一下。进入修改页面。
	 * 
	 * 否则提示用户信息填写错误不重写session值
	 * 
	 * 判断是否有$_GET;
	 * 判断参数是否正确：
	 * 不正确，返回
	 * 正确：
	 * 清除RegisterSession
	 * 允许用户填写信息
	 * 
	 * 判断session是否存在，如果存在则
	 * Enter description here ...
	 */
	
	private function testInfo() {
		/*ViewData::$form_data->StudentName = "方式";
		ViewData::$form_data->OldSchool = "海沧幼儿园";
		ViewData::$form_data->Sex = '2';
		ViewData::$form_data->Photo = '';
		ViewData::$form_data->BirthdayYear = 1990;
		ViewData::$form_data->BirthdayMonth = 1;
		ViewData::$form_data->Height = 33;
		ViewData::$form_data->IsOldStudent = 1;
		ViewData::$form_data->School = '莲花中学';
		ViewData::$form_data->Weight = 4;
		ViewData::$form_data->FirstParentName = '涨十年';
		ViewData::$form_data->FirstMobile = '15259267958';
		ViewData::$form_data->FirstParentProfession = '公务员';
		ViewData::$form_data->SecondMobile = '15259267959';
		ViewData::$form_data->SecondParentName = '弄名';
		ViewData::$form_data->SecondParentProfession = '弄名';
		ViewData::$form_data->Address = '厦门市思明区阳台山17号801室';
		ViewData::$form_data->Words = '1';
		ViewData::$form_data->FromWay = array (0, 1, 2 );
		ViewData::$form_data->RefersReading = '1';
		ViewData::$form_data->Recite = '1';
		ViewData::$form_data->ReciteThousand = '1';
		ViewData::$form_data->ReadingInterest = '1';
		ViewData::$form_data->ReadingAbility = '1';
		ViewData::$form_data->Focus = '1';
		ViewData::$form_data->Language = '1';
		ViewData::$form_data->CharacterStudent = array (0, 1, 2 );
		ViewData::$form_data->Etiquette = '1';
		ViewData::$form_data->HabitMove = '1';
		ViewData::$form_data->HabitSitting = '1';
		ViewData::$form_data->HabitSay = '1';
		ViewData::$form_data->Life = array (1 );
		ViewData::$form_data->Hope = array (1 );
		ViewData::$form_data->HopeOther = '';
		ViewData::$form_data->Goods = '';
		ViewData::$form_data->Others = '';
		*/
		//=================保留===================================
		ViewData::$form_data->Healthy = '无';
		ViewData::$form_data->ActivityRestriction = '无';
		ViewData::$form_data->Requirements = '无';
		ViewData::$form_data->Participation = '无';
	}
	private function getCampTitle($gradeId) {
		$info = $this->campLib->getGradeByGradeId ( $gradeId );
		if (! $info) {
			return '';
		}
		if ($info->ParentId == 1) {
			return '夏令营报名';
		} else {
			return '冬令营报名';
		}
	}
	public function camp() {
		ViewData::$form_data = new RegisteCampClass ();
		ViewData::$form_hint = new RegisteCampClass ();
		$this->testInfo ();
		if ($_POST) {
			ViewData::$title = $this->getCampTitle ( UserSession::getRegisteInfo()->GradeId );
			$this->campLib->checkForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "registeCamp" );
			}
			try {
				$this->checkRegisteSession ( 1 );
				$studentId = $this->campLib->registeLogic ();
				$this->resetRegisteSession ( $studentId );
				Header ( "Location:  " . siteUrl ( "registe/campconfirm" ) );
				exit ();
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
				BaseAction::loadView ( "registeCamp" );
			}
		}
		if ($_GET) {
			try {
				$this->checkRegisteFlag ();
				ViewData::$title = $this->getCampTitle ( UserSession::getRegisteInfo()->GradeId );
				BaseAction::loadView ( "registeCamp" );
			} catch ( Exception $e ) {
				BaseAction::error ( "请不要非法访问" );
			}
		}
		BaseAction::error ( "请不要非法访问" );
	}
	
	/**
	 * 信息确认页面
	 * 判断信息是否正确，editflag是否存在
	 * 判断是否是$_post的，如果是则修改信息
	 * 修改成功则删除session	
	 * http://apple.zhaosheng.com/index.php/registe/campconfirm
	 */
	public function campconfirm() {
		$this->checkRegisteSession ( 1, 1 );
		ViewData::$title = $this->getCampTitle ( UserSession::getRegisteInfo()->GradeId );
		ViewData::$form_data = $this->campLib->getById ( $this->registerSession->StudentId );
		if (! ViewData::$form_data) {
			BaseAction::error ( "对不起请不要非法访问！" );
		}
		BaseAction::loadView ( 'registeCampConfirm' );
	}
	/**
	 * 信息修改页面
	 * http://apple.zhaosheng.com/index.php/registe/editcamp
	 */
	public function campedit() {
		$this->checkRegisteSession ( 1, 1 );
		ViewData::$title = $this->getCampTitle ( UserSession::getRegisteInfo()->GradeId );
		$studentInfo = $this->campLib->getById ( $this->registerSession->StudentId );
		ViewData::$form_data = $studentInfo;
		ViewData::$form_hint = new RegisteCampClass ();
		if ($_POST) {
			$this->campLib->checkEditForm ();
			ViewData::$form_data->GradeType = $studentInfo->GradeType;
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "registeCamp" );
			}
			try {
				$this->campLib->editLogic ( $this->registerSession->StudentId );
				Header ( "Location:  " . siteUrl ( "registe/campconfirm" ) . "" );
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		BaseAction::loadView ( "registeCamp" );
	}
	//========================全日制=============================================
	

	public function fulltime() {
		ViewData::$title = "厦门国谊学校入学报名";
		ViewData::$form_data = new RegisteFullTimeClass ();
		ViewData::$form_hint = new RegisteFullTimeClass ();
		$this->testInfo ();
		if ($_POST) {
			$this->fullTimeLib->checkForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "registeFullTime" );
			}
			try {
				$this->checkRegisteSession ( 3 );
				$studentId = $this->fullTimeLib->registeLogic ();
				$this->resetRegisteSession ( $studentId );
				Header ( "Location:  " . siteUrl ( "registe/FullTimeconfirm" ) );
				exit ();
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		if ($_GET) {
			try {
				$this->checkFullTimeRegisterFlag ();
				BaseAction::loadView ( "registeFullTime" );
			} catch ( Exception $e ) {
				BaseAction::error ( "请不要非法访问" );
			}
		
		}
		BaseAction::error ( "请不要非法访问" );
	}
	
	/**
	 * 信息确认页面
	 * 判断信息是否正确，editflag是否存在
	 * 判断是否是$_post的，如果是则修改信息
	 * 修改成功则删除session	
	 * http://apple.zhaosheng.com/index.php/registe/campconfirm
	 */
	public function fulltimeconfirm() {
		$this->checkRegisteSession ( 3, 1 );
		ViewData::$title = "厦门国谊学校入学报名";
		ViewData::$form_data = $this->fullTimeLib->getById ( $this->registerSession->StudentId );
		if (! ViewData::$form_data) {
			BaseAction::error ( "对不起请不要非法访问！" );
		}
		BaseAction::loadView ( 'registeFullTimeConfirm' );
	}
	/**
	 * 信息修改页面
	 * http://apple.zhaosheng.com/index.php/registe/editcamp
	 */
	public function fulltimeedit() {
		$this->checkRegisteSession ( 3, 1 );
		ViewData::$title = "厦门国谊学校入学报名";
		$studentInfo = $this->fullTimeLib->getById ( $this->registerSession->StudentId );
		ViewData::$form_data = $studentInfo;
		ViewData::$form_hint = new RegisteFullTimeClass ();
		if ($_POST) {
			$this->fullTimeLib->checkEditForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "registeFullTime" );
			}
			try {
				$this->fullTimeLib->editLogic ( $this->registerSession->StudentId );
				Header ( "Location:  " . siteUrl ( "registe/fullTimeconfirm" ) . "" );
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		BaseAction::loadView ( "registefullTime" );
	}
	
	//======================莲花==================================================
	public function lotus() {
		ViewData::$title = "莲花书院入学报名";
		ViewData::$form_data = new RegisteLotusClass ();
		ViewData::$form_hint = new RegisteLotusClass ();
		$this->testInfo ();
		if ($_POST) {
			$this->lotusLib->checkForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "registeLotus" );
			}
			try {
				$this->checkRegisteSession ( 4 );
				$studentId = $this->lotusLib->registeLogic ();
				$this->resetRegisteSession ( $studentId );
				Header ( "Location:  " . siteUrl ( "registe/lotusconfirm" ) );
				exit ();
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		if ($_GET) {
			try {
				$this->checkFullTimeRegisterFlag ();
				BaseAction::loadView ( "registeLotus" );
			} catch ( Exception $e ) {
				BaseAction::error ( "请不要非法访问" );
			}
		}
		BaseAction::error ( "请不要非法访问" );
	}
	
	/**
	 * 信息确认页面
	 * 判断信息是否正确，editflag是否存在
	 * 判断是否是$_post的，如果是则修改信息
	 * 修改成功则删除session	
	 * http://apple.zhaosheng.com/index.php/registe/campconfirm
	 */
	public function lotusconfirm() {
		$this->checkRegisteSession ( 4, 1 );
		ViewData::$title = "莲花书院入学报名";
		ViewData::$form_data = $this->lotusLib->getById ( $this->registerSession->StudentId );
		if (! ViewData::$form_data) {
			BaseAction::error ( "对不起请不要非法访问！" );
		}
		BaseAction::loadView ( 'registeLotusConfirm' );
	}
	/**
	 * 信息修改页面
	 * http://apple.zhaosheng.com/index.php/registe/editcamp
	 */
	public function lotusedit() {
		$this->checkRegisteSession ( 4, 1 );
		ViewData::$title = "莲花书院入学报名";
		ViewData::$form_data = $this->lotusLib->getById ( $this->registerSession->StudentId );
		ViewData::$form_hint = new RegisteLotusClass ();
		if ($_POST) {
			$this->lotusLib->checkEditForm ();
			if (! BaseAction::isObjectEmpty ( ViewData::$form_hint )) {
				BaseAction::loadView ( "registeFullTime" );
			}
			try {
				$this->lotusLib->editLogic ( $this->registerSession->StudentId );
				Header ( "Location:  " . siteUrl ( "registe/lotusconfirm" ) . "" );
			} catch ( Exception $e ) {
				ViewData::$form_hint->Message = $e->getMessage ();
			}
		}
		BaseAction::loadView ( "registeLotus" );
	}
	
	//=====================common==================================================
	public function registesuccess() {
		if (! isset ( $this->registerSession->Channel )) {
			BaseAction::error ( "请不要非法访问！" );
		}
		
		ViewData::$title = "报名成功等待审核";
		
		if ($this->registerSession->Channel == 1 || $this->registerSession->Channel == 2) {
			$this->checkRegisteSession ( 1, 1 );
			if (! $this->campLib->addRegistedNumber ( $this->registerSession->GradeId, $this->registerSession->GradeType )) {
				BaseAction::error ( "对不起，报名人数已满，不能继续报名！" );
			}
			$info->RegistStatus = 1;
			$info->RegistTime = date ( 'Y-m-d H:i:s' );
			$this->campLib->setInfoById ( $info, $this->registerSession->StudentId );
			UserSession::clearRegisteInfo ();
			BaseAction::loadView ( 'registeCampSuccess' );
		}
		if ($this->registerSession->Channel == 3) {
			$this->checkRegisteSession ( 3, 1 );
			if (! $this->fullTimeLib->addRegistedNumber ( $this->registerSession->GradeId )) {
				BaseAction::error ( "对不起，报名人数已满，不能继续报名！" );
			}
			$info->RegistStatus = 1;
			$info->RegistTime = date ( 'Y-m-d H:i:s' );
			$this->fullTimeLib->setInfoById ( $info, $this->registerSession->StudentId );
			UserSession::clearRegisteInfo ();
			BaseAction::loadView ( 'RegisteFullTimeSuccess' );
		}
		if ($this->registerSession->Channel = 4) {
			$this->checkRegisteSession ( 4, 1 );
			if (! $this->lotusLib->addRegistedNumber ( $this->registerSession->GradeId )) {
				BaseAction::error ( "对不起，报名人数已满，不能继续报名！" );
			}
			$info->RegistStatus = 1;
			$info->RegistTime = date ( 'Y-m-d H:i:s' );
			$this->lotusLib->setInfoById ( $info, $this->registerSession->StudentId );
			UserSession::clearregisteInfo ();
			BaseAction::loadView ( 'registeLotusSuccess' );
		}
	}
	
	/**
	 * 判断$_GET的参数是否正确，不正确就报错
	 * 清除RegisterSession
	 * 设置RegisterSession
	 */
	private function checkRegisteFlag() {
		$info = new RegisteSession ();
		BaseAction::parseForm ( "GET", $info );
		BaseAction::parseForm ( "GET", ViewData::$form_data );
		BaseAction::checkForm ( 'Channel', 'GET', '参数不能为空', array (1, 1, 4, "参数错误" ), FALSE, FALSE );
		BaseAction::checkForm ( 'GradeId', 'GET', '参数不能为空', array (1, 1, 100, "参数错误" ), FALSE, FALSE );
		BaseAction::checkForm ( 'GradeType', 'GET', '参数不能为空', array (1, 1, 3, "参数错误" ), FALSE, FALSE );
		UserSession::clearRegisteInfo ();
		UserSession::setRegisteInfo ( $info );
		return;
	}
	
	private function checkFullTimeRegisterFlag() {
		$info = new RegisteSession ();
		BaseAction::parseForm ( "GET", $info );
		BaseAction::parseForm ( "GET", ViewData::$form_data );
		BaseAction::checkForm ( 'Channel', 'GET', '参数不能为空', array (1, 1, 4, "参数错误" ), FALSE, FALSE );
		BaseAction::checkForm ( 'GradeId', 'GET', '参数不能为空', array (1, 1, 100, "参数错误" ), FALSE, FALSE );
		UserSession::clearRegisteInfo ();
		UserSession::setRegisteInfo ( $info );
		return;
	}
	/**
	 * 在RegisterSession中添加学会Id并且把EditFlag变成1
	 */
	private function resetRegisteSession($studentId) {
		$value = $this->registerSession;
		$value->StudentId = $studentId;
		$value->EditFlag = 1;
		UserSession::setRegisteInfo ( $value );
	}
	/**
	 * 判断是否可以进入确认修改的页面
	 */
	private function checkRegisteSession($channel = FALSE, $editFlag = FALSE) {
		
		if (! $this->registerSession) {
			BaseAction::error ( "请不要非法访问" );
		}
		if ($editFlag) {
			if (! $this->registerSession->StudentId) {
				BaseAction::error ( "请不要非法访问" );
			}
			if ($this->registerSession->EditFlag != 1) {
				BaseAction::error ( "请不要非法访问" );
			}
		}
		if (! $channel) {
			return TRUE;
		}
		switch ($channel) {
			case 1 :
				if ($this->registerSession->Channel == 1 || $this->registerSession->Channel == 2)
					
					return;
				break;
			case 2 :
				if ($this->registerSession->Channel == 1 && $this->registerSession->Channel == 2)
					return;
				break;
			case 3 :
				if ($this->registerSession->Channel == 3)
					return;
				break;
			case 4 :
				if ($this->registerSession->Channel == 4)
					return;
				break;
			default :
				BaseAction::error ( "请不要非法访问" );
				break;
		}
		BaseAction::error ( "请不要非法访问" );
	}
}